package tr.com.netgis;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class GPSDemo extends Activity {
	TextView tv = null;
	GPS mGPS = null;;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		tv = new TextView(getApplicationContext());
		setContentView(tv);
	}

	@Override
	protected void onPause() {
		super.onPause();
		if (mGPS != null) {
			mGPS.stopLocationUpdates();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		mGPS = new GPS(this.getApplicationContext());
	}

	// /////////////////////////
	class GPS implements LocationListener { // as inner class...
		LocationManager lm = null;
		Location actualLocation = null;

		public GPS(Context context) {
			lm = (LocationManager) context
					.getSystemService(Context.LOCATION_SERVICE);
			if (lm != null) {
				actualLocation = lm
						.getLastKnownLocation(LocationManager.GPS_PROVIDER);
			}
			startLocationUpdates();
		}

		public void startLocationUpdates() {
			lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500l, 0,
					this);
		}

		public void stopLocationUpdates() {
			lm.removeUpdates(this);
		}

		public void onLocationChanged(Location updatedLocation) {
			actualLocation = updatedLocation;
			String gpsDetails = getGPSString();
			if (gpsDetails != null) {
				tv.setText(gpsDetails);
			}
		}

		public void onProviderEnabled(String provider) {
		}

		public void onProviderDisabled(String provider) {
		}

		public void onStatusChanged(String provider, int iUnknown,
				Bundle buUnknown) {
		}

		public String getGPSString() {
			String clock = "C" + new java.util.Date().toString();
			String loc = null;
			if (actualLocation != null) {
				StringBuffer sb = new StringBuffer();
				sb.append("N"
						+ Location.convert(actualLocation.getLatitude(),
								Location.FORMAT_SECONDS));
				sb.append(" E"
						+ Location.convert(actualLocation.getLongitude(),
								Location.FORMAT_SECONDS));
				// if (actualLocation.hasAltitude() == true)
				{
					sb.append(" A" + actualLocation.getAltitude());
				}
				// if (actualLocation.hasAccuracy() == true)
				{
					sb.append(" D" + round(actualLocation.getAccuracy()));
				}
				// if (actualLocation.hasSpeed() == true)
				{
					float kph = round((float) (actualLocation.getSpeed() / 0.277)); // converts
																					// m/s
																					// to
																					// kph
					sb.append(" S" + round(actualLocation.getSpeed()) + "m "
							+ kph + "k");
				}
				// if (actualLocation.hasBearing() == true)
				{
					sb.append(" B" + round(actualLocation.getBearing()));
				}
				sb.append(" T"
						+ new java.util.Date(actualLocation.getTime())
								.toString());
				sb.append(" " + clock);
				loc = sb.toString();
			} else {
				loc = clock;
			}
			return loc;
		}

		public Location getUpdatedGPSLocation() {
			actualLocation = lm
					.getLastKnownLocation(LocationManager.GPS_PROVIDER);
			return actualLocation;
		}

		public Location getGPSLocation() {
			return actualLocation;
		}

		public long getLocationAge() {
			if (actualLocation != null) {
				return (System.currentTimeMillis() - actualLocation.getTime());
			} else {
				return (0);
			}
		}

		public float getBearing() {
			if (actualLocation != null && actualLocation.hasBearing() == true) {
				return (actualLocation.getBearing());
			} else {
				return (-1);
			}
		}

		public float getSpeed() {
			if (actualLocation != null && actualLocation.hasSpeed() == true) {
				return (actualLocation.getSpeed());
			} else {
				return (-1);
			}
		}

		private float round(float toRound) {
			int rounded = (int) ((float) toRound * 10.0f);
			return (((float) rounded) / 10.0f);
		}
	}
}